package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.wxmp.service.WxUserService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Edward
 */
@AllArgsConstructor
@Controller
@RequestMapping("/wx/redirect/")
public class WxRedirectController {
    private final WxMpService wxService;

    private WxUserService wUserService;
    @RequestMapping("/getOauthUserInfo")
    @ResponseBody
    public AjaxResult getOauthUserInfo(@RequestParam String appId, @RequestParam String code) {
        if (!this.wxService.switchover(appId)) {
            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", appId));
        }
        try {
            WxMpUser wUser = wUserService.queryByOpenid(wUserService.queryOpenidByCode(code));
            return new AjaxResult(200, "获取微信用户成功", wUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
